﻿<%@ WebHandler Language="C#" Class="list" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using System.Web.Services;
using System.IO;
using System.Drawing;


public class list : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    private StringBuilder sb = null;
    public DataSet ds = null;
    public DataTable dt = null;
    private string sqlwhere = "";
    public void ProcessRequest (HttpContext context) {
        string type = PageBase.ext_string(context.Request.Params["type"]);
        switch (type)
        {
            //查询类型下拉选项
            case "getselect":
                getselect(context);
                break;
            //查询菜单信息
            case "getlist":
                getinfo_list(context);
                break;
            //新增
            case "add":
                add(context);
                break;
            //修改
            case "edit":
                edit(context);
                break;
            //删除
            case "delete":
                delete(context);
                break;
            //启用
            case "start":
                start(context);
                break;
            //根据id获取信息
            case "getlist_byid":
                getlist_byid(context);
                break;
            default:
                //getinfo(context);
                break;
        }
    }

    //查询根据id
    public void getlist_byid(HttpContext context)
    {
        string editid = PageBase.ext_string(context.Request.Params["editid"]);
        string sql = @"select a.id, a.menuname,b.menuname parentmenuname, a.menuurl, a.menupic, a.menulevel, a.parentid, a.xh, a.remark, 
      ltrim(rtrim(a.isactive)) isactive, a.createtime, a.updatetime from [sys_menus] a
	  left join [sys_menus] b on a.parentid=b.id
	   where a.id ={0}";

        sql = string.Format(sql,editid);
        DataTable dt = DBHelper.ExecuteDataTable(sql);
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string urlgsh = "../../" + dt.Rows[i]["menupic"];
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"parentmenuname\":\"" + dt.Rows[i]["parentmenuname"] + "\"");
            sb.Append(",\"title\":\"" + dt.Rows[i]["menuname"] + "\"");
            sb.Append(",\"menuurl\":\"" + dt.Rows[i]["menuurl"] + "\"");
            sb.Append(",\"xh\":\"" + dt.Rows[i]["xh"] + "\"");
            sb.Append(",\"menulevel\":\"" + dt.Rows[i]["menulevel"] + "\"");
            sb.Append(",\"remark\":\"" + dt.Rows[i]["remark"] + "\"");
            sb.Append(",\"pid\":\"" + dt.Rows[i]["parentid"] + "\"");
            sb.Append(",\"imgurl\":\"" + urlgsh + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"");
            sb.Append(",\"updatetime\":\"" + dt.Rows[i]["updatetime"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //启用
    public void start(HttpContext context)
    {
        string getids = PageBase.ext_string(context.Request.Params["getids"]);
        string msg = "";
        bool flag;
        string sqldelete = "update  sys_menus set isactive='Y' where id in ({0})";
        sqldelete = string.Format(sqldelete, getids);
        int dtcount= DBHelper.ExecuteNonQuery(sqldelete);
        if (dtcount > 0)
        {
            flag = true;
            msg = "启用成功";
        }
        else
        {
            flag = false;
            msg = "启用失败";

        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //删除
    public void delete(HttpContext context)
    {
        string getids = PageBase.ext_string(context.Request.Params["getids"]);
        string msg = "";
        bool flag;
        string sqldelete = "update  sys_menus set isactive='N' where id in ({0})";
        sqldelete = string.Format(sqldelete, getids);
        int dtcount= DBHelper.ExecuteNonQuery(sqldelete);
        if (dtcount > 0)
        {
            flag = true;
            msg = "禁用成功";
        }
        else
        {
            flag = false;
            msg = "禁用失败";

        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }

    //查询
    public void getinfo_list(HttpContext context)
    {
        string where = @"select  COUNT(*) OVER(PARTITION BY 1) AS TOTAL,id, menuname, menuurl, menupic, xh, menulevel,
          ltrim(rtrim(isactive)) isactive, remark, parentid,createtime, updatetime from [sys_menus]  where 1=1 order by xh";

        DataTable dt = DBHelper.ExecuteDataTable(where);
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());
        sb.Append("[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string urlgsh = "../../" + dt.Rows[i]["menupic"];
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"title\":\"" + dt.Rows[i]["menuname"] + "\"");
            sb.Append(",\"menuurl\":\"" + dt.Rows[i]["menuurl"] + "\"");
            sb.Append(",\"xh\":\"" + dt.Rows[i]["xh"] + "\"");
            sb.Append(",\"menulevel\":\"" + dt.Rows[i]["menulevel"] + "\"");
            sb.Append(",\"remark\":\"" + dt.Rows[i]["remark"] + "\"");
            sb.Append(",\"pid\":\"" + dt.Rows[i]["parentid"] + "\"");
            sb.Append(",\"imgurl\":\"" + urlgsh + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"");
            sb.Append(",\"updatetime\":\"" + dt.Rows[i]["updatetime"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //查询菜单下拉选项
    public void getselect(HttpContext context)
    {
        string menuid = PageBase.ext_string(context.Request.Params["menuid"]);
        string sql = @"select * from sys_menus where id<>{0} and ltrim(rtrim(isactive))='Y'";
        sql = string.Format(sql,menuid);
        DataTable dt = DBHelper.ExecuteDataTable(sql);

        string fsmessage = "";
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        sb.Append("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"menuname\":\"" + dt.Rows[i]["menuname"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //修改
    public void edit(HttpContext context)
    {
        string msg = "";
        string sqlsave = "";
        bool flag;
        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }
        string id = Utility.Trim(list.LoadData_type[0].id);
        string parentmenuid = Utility.Trim(list.LoadData_type[0].parentmenuid);
        string menuname = Utility.Trim(list.LoadData_type[0].menuname);
        string menuurl = Utility.Trim(list.LoadData_type[0].menuurl);
        int? menuxh = Utility.ToInt(list.LoadData_type[0].menuxh);
        string menuremark = Utility.Trim(list.LoadData_type[0].menuremark);
        string level = Utility.Trim(list.LoadData_type[0].level);
        string isactive = Utility.Trim(list.LoadData_type[0].ISACTIVE);
        string isactive_ = isactive == "True" ? "Y" : "N";

        sqlsave = @"update [sys_menus] set 
                                   [menuname]='{0}'
                                  ,[menuurl]='{1}'
                                  ,[menulevel]='{2}'
                                  ,[parentid]='{3}'
                                  ,[xh]='{4}'
                                  ,[remark]='{5}'
                                  ,[isactive]='{6}'
                                  ,[updatetime]='{7}'
                                   where id={8};";
        sqlsave = string.Format(sqlsave, menuname, menuurl, level, parentmenuid, menuxh, menuremark, isactive_, DateTime.Now.ToString(), id);
        int zxcount = DBHelper.ExecuteNonQuery(sqlsave);
        if (zxcount > 0)
        {
            flag = true;
            msg = "修改成功";
        }
        else
        {
            flag = false;
            msg = "修改失败，请联系管理员";
        }

        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //新增
    public void add(HttpContext context)
    {
        string msg = "";
        string sqlsave = "";
        bool flag;
        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }
        string parentmenuname = Utility.Trim(list.LoadData_type[0].parentmenuname);
        string parentmenuid = Utility.Trim(list.LoadData_type[0].parentmenuid);
        string menuname = Utility.Trim(list.LoadData_type[0].menuname);
        string menuurl = Utility.Trim(list.LoadData_type[0].menuurl);
        int? menuxh = Utility.ToInt(list.LoadData_type[0].menuxh);
        string menuremark = Utility.Trim(list.LoadData_type[0].menuremark);
        string level = Utility.Trim(list.LoadData_type[0].level);

        string isactive = Utility.Trim(list.LoadData_type[0].ISACTIVE);
        string isactive_ = isactive == "True" ? "Y" : "N";

        //string ishave = "select * from sys_menus where menuname='{0}' and ltrim(rtrim(isactive))='Y'";
        //ishave = string.Format(ishave, menuname);
        //DataTable dtcount= DBHelper.ExecuteDataTable(ishave);
        //if (dtcount.Rows.Count > 0)
        //{
        //    flag = false;
        //    msg = "已存在此菜单名称，请修改";
        //}
        //else
        //{
        sqlsave = @"INSERT INTO [sys_menus]
                                   ([menuname]
                                   ,[menuurl]
                                   ,[menulevel]
                                   ,[xh]
                                   ,[remark]
                                   ,[parentid]
                                   ,[isactive]
                                   ,[createtime]
                                   ,[updatetime]
                                   )
                             VALUES
                                   ('{0}'
                                   ,'{1}'
                                   ,'{2}'
                                   ,'{3}'
                                   ,'{4}'
                                   ,'{5}'
                                   ,'{6}'
                                   ,'{7}'
                                   ,'{8}'
                                  );";
        sqlsave = string.Format(sqlsave, menuname, menuurl, level, menuxh, menuremark, parentmenuid, isactive_, DateTime.Now.ToString(), DateTime.Now.ToString());
        int zxcount = DBHelper.ExecuteNonQuery(sqlsave);
        if (zxcount > 0)
        {
            flag = true;
            msg = "新增成功";
        }
        else
        {
            flag = false;
            msg = "新增失败，请联系管理员";
        }
        //}
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    public class GetDate
    {
        public List<menumanager> LoadData_type { get; set; }
    }
    public class menumanager
    {
        //id
        public string id;
        public string ID
        {
            get { return id; }
            set { id = value; }
        }
        public string parentmenuname;
        public string PARENTMENUNAME
        {
            get { return parentmenuname; }
            set { parentmenuname = value; }
        }
        public string parentmenuid;
        public string PARENTMENUID
        {
            get { return parentmenuid; }
            set { parentmenuid = value; }
        }
        public string menuname;
        public string MENUNAME
        {
            get { return menuname; }
            set { menuname = value; }
        }
        public string menuurl;
        public string MENUURL
        {
            get { return menuurl; }
            set { menuurl = value; }
        }
        public int? menuxh;
        public int? MENUXH
        {
            get { return menuxh; }
            set { menuxh = value; }
        }
        public string menuremark;
        public string IMGURL
        {
            get { return menuremark; }
            set { menuremark = value; }
        }
        public string level;
        public string LEVEL
        {
            get { return level; }
            set { level = value; }
        }

        public string isactive;
        public string ISACTIVE
        {
            get { return isactive; }
            set { isactive = value; }
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}